<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Date: 2022/10/19
 * Time: 10:45
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SaleBillLog extends Model
{
    use HasFactory;

    protected $table = 'sp_sale_bill_log';
    protected $primaryKey = 'id';
    public $timestamps = false;
    
    //账单类型
    const MONEY_GET = 1;  //收入
    const MONEY_PUT = 2;  //提现
    
    //查询流水记录
    protected function getBillList($pageSize,$sale_user_id,$where=[])
    {
        $list = $this
            ->where('sale_user_id',$sale_user_id)
            ->where($where)
            ->orderBy('create_time','desc')
            ->paginate($pageSize);
        $list = obToArray($list);
        if(isset($list['data']) && $list['data']){
            foreach ($list['data'] as $k=>$item){
                //账单类型
                $list['data'][$k]['typeName'] = $item['type'] == self::MONEY_GET ? '收入' : '提现';
            }
        }
        return $list;
    }
    
    //添加流水记录
    protected function setBillList($request)
    {
        $data = [];
        $data['sale_user_id'] = $request['sale_user_id'];
        $data['content'] = $request['content'];
        $data['balance_before'] = $request['balance_before'];
        $data['balance_after'] = $request['balance_after'];
        $data['money'] = $request['money'];
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['type'] =  $request['type']; //操作类型1收成2提现	
        $data['remark'] =  $request['remark'];
        SaleBillLog::insert($data);
    }
}
